﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ActivtyUI.App_Code
{
    public class ActCommon
    {


        public static object lockObj = new object();
        public static SendRedPackConfig GetSendRedPackConfigByActName(string actName)
        {
            #region//益盟股票分析
            SendRedPackConfig config1 = new SendRedPackConfig()
            {
                act_name = "",
                mch_id = "1225461202",
                paykey = "emoney2015chaoguquanweixinfuwuha",
                remark = "",
                share_content = "",
                wishing = "",
                title = "益盟股票分析",
                wechat_bag_cert = System.Configuration.ConfigurationManager.AppSettings["wechat_bag_cert"],
                wxappid = "wx25bf1ed19bab0096"
            };
            #endregion

            #region 股市最新消息
            SendRedPackConfig config4 = new SendRedPackConfig()
            {
                act_name = "",
                mch_id = "1283843701",   
                paykey = "34df3232932k90932jkskldfjsdjfk92",
                remark = "",
                share_content = "",
                wishing = "",
                title = "股市最新消息",
                wechat_bag_cert = System.Configuration.ConfigurationManager.AppSettings["wechat_bag_certZX"],
                wxappid = "wx9b50eda201b7c463"
            };
            #endregion

             #region A股市场
            SendRedPackConfig config5 = new SendRedPackConfig()
            {
                act_name = "",
                mch_id = "1283867301",
                paykey = "23sdfsdfsd423423423434324234weed",
                remark = "",
                share_content = "",
                wishing = "",
                title = "A股市场",
                wechat_bag_cert = System.Configuration.ConfigurationManager.AppSettings["wechat_bag_certAG"],
                wxappid = "wx09818a2ddc0fef18"
            };
            #endregion


            #region 益盟操盘手订阅
            SendRedPackConfig config2 = new SendRedPackConfig()
            {

                act_name = "",
                mch_id = "1268655901",
                paykey = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ikp",
                remark = "",
                share_content = "",
                wishing = "",
                title = "益盟操盘手订阅",
                wechat_bag_cert = System.Configuration.ConfigurationManager.AppSettings["wechat_bag_cert2"],
                wxappid = "wx07cd11360d0f5afb",

            };
            #endregion

            #region   益盟操盘手

            SendRedPackConfig config3 = new SendRedPackConfig()
            {

                act_name = "",
                mch_id = "1220301801",
                paykey = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ikp",
                remark = "",
                share_content = "",
                wishing = "",
                title = "益盟操盘手",
                wechat_bag_cert = "E:\\cert\\caopanshou\\apiclient_cert.p12",
                wxappid = "wxd14f20f92318c816"
            };
            #endregion



            switch (actName)
            {
                case "DailyTaskHGP":
                    config1.wishing = "恭喜发财，万事如意！";
                    config1.act_name = "签到有奖";
                    config1.remark = "积分兑换抽奖";
                    return config1;

                case "DailyTask2GSZX":
                    config4.wishing = "恭喜发财，万事如意！";
                    config4.act_name = "签到有奖";
                    config4.remark = "积分兑换抽奖";
                    return config4;

                case "DailyTaskAGCS":
                    config5.wishing = "恭喜发财，万事如意！";
                    config5.act_name = "签到有奖";
                    config5.remark = "积分兑换抽奖";
                    return config5;

                #region   关于画元旦活动的配置
                case "HuanYuandanHGP":
                    config1.wishing = "恭喜发财，万事如意！";
                    config1.act_name = "画元旦活动";
                    config1.remark = "画元旦活动抽奖";
                    return config1;

                case "HuanYuandanGSZX":
                    config4.wishing = "恭喜发财，万事如意！";
                    config4.act_name = "画元旦活动";
                    config4.remark = "画元旦活动抽奖";
                    return config4;

                case "HuanYuandanAGCS":
                    config5.wishing = "恭喜发财，万事如意！";
                    config5.act_name = "画元旦活动";
                    config5.remark = "画元旦活动抽奖";
                    return config5;
                #endregion

            }
  
            return null;

        }

        /// <summary>
        /// 根据openid获取一个用户的信息，从微信接口中取
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static WeixinUserInfo_ReturnJson GetUserInfoByOpenId(string openid, string appid, string appsecret)
        {
            string url = string.Format(Config.WeiXin_ApiUrl + "user/info?access_token={0}&openid={1}&lang=zh_CN", TokenHelper.GetToken(appid, appsecret), openid);
            string json = NetHelper.LoadData(url, null);
            if (json.Contains("{\"errcode\":40001"))
            {
                lock (lockObj)
                {
                    TokenHelper.ClearToken(appid);
                }
                url = string.Format(Config.WeiXin_ApiUrl + "user/info?access_token={0}&openid={1}&lang=zh_CN", TokenHelper.GetToken(appid, appsecret), openid);
                json = NetHelper.LoadData(url, null);
            }
            WeixinUserInfo_ReturnJson user = JsonTools.ToObject<WeixinUserInfo_ReturnJson>(json);
            return user;
        }


    }
}